import psutil
from tqdm import tqdm
from common.logger import logger
import time


def cpu_utilization_percentage(monitor_sec: int = 10):
    """Return the CPU utilization in percentage.

    Parameters
    ----------
    monitor_sec : int, optional
        Monitoring time of CPU utilization in seconds, by default 10
    """
    cpu_percent = 0
    logger.info(f"Monitoring CPU Utilization {monitor_sec} seconds...")
    for _ in tqdm(range(monitor_sec)):
        cpu_percent += psutil.cpu_percent()
        time.sleep(1)

    average_cpu_percent = int(0.5 + cpu_percent / monitor_sec)
    logger.info(f"Average CPU utilization is {average_cpu_percent}%")
    return average_cpu_percent


if __name__ == "__main__":
    cpu_utilization_percentage()
